// pages/cart/index.js.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    values: 1,
    value: 1,
    //控制隐藏显示
    buttomshow: [{
      checks: true,
      value: 0
    }, {
      checks: true,
      value: 0
    }, {
      checks: true,
      value: 0
    }, {
      checks: true,
      value: 0
    }, {
      checks: true,
      value: 0
    }],
    gooodlist: [],
    nums: {
      goodnum: 1,
      goodnums: 1,
      goodnumss: 1,
      goodnumssss:1,
    },
    prices: [],
    pricenums: 0,
    numpr: 0,
    numpt: 0,
    numpts: 0,
    baoyang:0,
    upmoney: 600, //升级的钱
    judge: 0,
    payData: [],
    countOnChanges: 0,
    countOnChange: 0,
    countOnChangess: 0,
    show: false,
    showAddOrder: false,
    money: 0,
    //根据当前等级打折价格
    pricone: 0,
    prictwo: 0,
    userinfo: '',
    fuoumoney: 0,
    //折扣
    play: 1,
    oldpric: 0,
    allnum:1,
    //购物车数量
    count:0,
    lev:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var dengji = wx.getStorageSync('loginuser');
    console.log(dengji);
    //计算升下一级需要的钱
    if (dengji.levelName == '会员') {
      // this.data.fuoumoney=9000-dengji.repurchaseSpend
      console.log(this.data.fuoumoney);
      this.setData({
        fuoumoney: 9000 - dengji.repurchaseSpend,
        allnum:1
      })

    } else if (dengji.levelName == '网点') {
      console.log(this.data.fuoumoney);

      this.setData({
        fuoumoney: 50000 - dengji.repurchaseSpend,
        allnum:2
      })
    }else if (dengji.levelName == '推广站') {
      this.setData({
        fuoumoney: 50000 - dengji.repurchaseSpend,
        allnum:2
      })
    }
     else  if (dengji.levelName == '总代') {
      this.setData({
        lev:5,
        fuoumoney: 0,
        allnum:2
      })
    }else{
      this.setData({
        lev:6,
        fuoumoney: 0,
        allnum:2
      })
    }
    this.setData({
      money: options.money
    })
    wx.request({
      url: app.globalData.url+'/zkxj/good/listbydiscount',
      data: {
        "currPage": 1,
        "pageSize": 10,
        "goodArea":1,
        "putawayStatus": 1,
        "goodAreaDetail":3,
	      "level":wx.getStorageSync('loginuser').level,
      },
      method: 'POST',
      header: {
        'content-type': 'application/json',
          'token':wx.getStorageSync('token')
      },
      success: (res) => {
        this.setData({
          gooodlist: res.data.data.list,
        });
        console.log(this.data.gooodlist);
      },
    })
   this.getcount()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  handletodelat(e){
    const index = e.currentTarget.dataset['index'];
    wx.navigateTo({
      url: `/pages/fugoudelate/index?id=${index}` 
  })
  },
  // 请求count
  getcount(){
    wx.request({
      url: app.globalData.url+'/zkxj/cart/area/countGood',
      data: {
        "goodAreaDetail": 3,
        "userId": wx.getStorageSync('userId'),
        "level":wx.getStorageSync('loginuser').level,
      },
      method: 'POST',
      header: {
        'content-type': 'application/json',
          'token':wx.getStorageSync('token')
      },
      success: (res) => {
        console.log(res);
        this.setData({
          count: res.data.count,
        });
        console.log(this.data.gooodlist);
      },
    })
  },
  //添加到购物车
  handleaddcart(e){
    const index = e.currentTarget.dataset['index'];
    const price = e.currentTarget.dataset['price'];
    console.log(index,price);
    const oldprice = e.currentTarget.dataset['oldpeice'];
wx.request({
  url: app.globalData.url+'/zkxj/cart/area/addNewGood',
  data: {
    "level":wx.getStorageSync('loginuser').level,
    "goodAreaDetail": 3,
	"goodId": index,
	"modifiedtime": "",
	"num":1,
	"oldPrice": oldprice,
	"price": price,
	"userId": wx.getStorageSync('userId')
  },
  method: 'POST',
  header: {
    'content-type': 'application/json',
      'token':wx.getStorageSync('token')
  },
  success: (res) => {
    console.log(res);
   if(res.data.msg=="商品添加成功！"){
    wx.showToast({
      title: '添加成功',
      icon: 'success',
      duration: 2000,
      mask: true
    })
    this.getcount()
   }else{
    wx.showToast({
      title: res.data.msg,
      icon: 'error',
      duration: 2000,
      mask: true
    })
   }
  },
})
  },
  //去购物车
  handletofugoucart(){
    wx.navigateTo({
      url: `/pages/fugoucart/index` 
  })
  },
  //去结算
  //去结算
  handletobuy() {
    var that = this.data
    if (this.data.payData.length == 0) {
      this.setData({
        showAddOrder: true
      });
      return
    }
    var sumMoney = that.numpr + that.numpt + this.data.numpts + that.pricenums+this.data.baoyang //总价
    var detailsData = JSON.stringify(this.data.payData);
    detailsData = encodeURIComponent(detailsData);
    wx.navigateTo({
      url: `/pages/seaccounts/index?arr=${detailsData}&sum=${JSON.stringify(sumMoney*this.data.play)}&type=2&upgradeLevel=0`
    })
    console.log(this.data.payData);

  },
  onCloses() {
    this.setData({
      show: false
    });
    this.setData({
      showAddOrder: false
    });
  },

  handlecheckbu(e) {
    var that = this.data
    const index = e.currentTarget.dataset['index'];
    const price = e.currentTarget.dataset['price'];
    console.log(price);
    console.log(index);
    console.log(price);
    this.data.buttomshow[index].checks = false
    this.setData({
      buttomshow: this.data.buttomshow,
      prices: this.data.price,
      pricenums: this.data.pricenums + price*this.data.allnum
    });
    console.log(this.data.pricenums,price);
    console.log(that.buttomshow[index].value);
    var count = that.buttomshow[index].value
    var money = that.pricenums
    console.log(this.data.allnum);
    if (index == 0) {
      that.payData.push({
        id: index + '',
        num: count || this.data.allnum,
        oldpric: that.gooodlist[0].originalPrice,
        price: (this.data.numpr || that.gooodlist[0].originalPrice) * this.data.play,
        name: that.gooodlist[0].name,
        coverPic: that.gooodlist[0].coverPic
      })
    } else if (index == 1) {
      that.payData.push({
        id: 2 + '',
        num: count ||this.data.allnum,
        oldpric: that.gooodlist[1].originalPrice,
        price: (this.data.numpt || that.gooodlist[1].originalPrice) * this.data.play,
        name: that.gooodlist[1].name,
        coverPic: that.gooodlist[1].coverPic
      })
    }else if (index == 2) {
      that.payData.push({
        id:3+ '',
        num: count ||this.data.allnum,
        oldpric: that.gooodlist[2].originalPrice,
        price: (this.data.numpt || that.gooodlist[2].originalPrice) * this.data.play,
        name: that.gooodlist[2].name,
        coverPic: that.gooodlist[2].coverPic
      })
    }else if (index == 3) {
      that.payData.push({
        id: 7+ '',
        num: count ||this.data.allnum,
        oldpric: that.gooodlist[1].originalPrice,
        price: (this.data.numpt || that.gooodlist[4].originalPrice) * this.data.play,
        name: that.gooodlist[4].name,
        coverPic: that.gooodlist[4].coverPic
      })
    }else{
      that.payData.push({
        id: index + '',
        num: count || this.data.allnum,
        oldpric: that.gooodlist[2].originalPrice,
        price: (this.data.numpts || that.gooodlist[2].originalPrice) * this.data.play,
        name: that.gooodlist[2].name,
        coverPic: that.gooodlist[2].coverPic
      })
    }
    console.log(this.data.payData);
  },
  onChange(event) {
    var that = this.data
    this.setData({
      nums: {
        goodnum: event.detail, //数量
      },
      numpr: (event.detail-this.data.allnum) * that.gooodlist[0].originalPrice //价格
    })
    console.log(this.data.numpr);
    if (that.nums.goodnum == 0) {
      that.countOnChange = 1;
      for (var i = 0; i < that.payData.length; i++) {
        if (that.payData[i].id == '0') {
          that.payData.splice(i, 1);
        }
      }
      // console.log(that.payData)
    } else {
      if (that.payData.length == 0 || that.countOnChange == 1) {
        that.payData.push({
          id: '0',
          num: 1,
          oldpric: that.gooodlist[0].originalPrice,
          price: that.gooodlist[0].originalPrice * this.data.play,
          name: that.gooodlist[0].name,
          coverPic: that.gooodlist[0].coverPic
        })
        that.countOnChange = 0;
      }
      for (var i = 0; i < that.payData.length; i++) {
        if (that.payData[i].id == '0') {
          that.payData[i].num = that.nums.goodnum
          that.payData[i].price = that.gooodlist[0].originalPrice * this.data.play
        }
      }
      // console.log(that.payData)
    }
  },
  onChanges(event) {
    var that = this.data
    console.log(that.gooodlist[1].originalPrice);
    this.setData({
      nums: {
        goodnums: event.detail
      },
      numpt:  (event.detail-this.data.allnum )  * that.gooodlist[1].originalPrice
    })
    if (that.nums.goodnums == 0) {
      that.countOnChanges = 1;
      for (var i = 0; i < that.payData.length; i++) {
        if (that.payData[i].id == 2) {
          that.payData.splice(i, 1);
        }
      }
      // console.log(that.payData)
    } else {
      if (that.payData.length == 0 || that.countOnChanges == 1) {
        that.payData.push({
          id: '2',
          num: 1,
          oldpric: that.gooodlist[1].originalPrice,
          price: that.gooodlist[1].originalPrice * this.data.play,
          name: that.gooodlist[1].name,
          coverPic: that.gooodlist[1].coverPic
        })
        that.countOnChanges = 0;
      }
      console.log( that.nums.goodnums);
      for (var i = 0; i < that.payData.length; i++) {
        if (that.payData[i].id == '2') {
          that.payData[i].num = that.nums.goodnums
          that.payData[i].price =  that.gooodlist[1].originalPrice * this.data.play
        }
      }
      // console.log(that.payData)
    }
  },
  onChangess(event) {
    var that = this.data
    console.log(event.detail,that.gooodlist);
    this.setData({
      nums: {
        goodnumss: event.detail
      },
      numpts: (event.detail-this.data.allnum ) * that.gooodlist[2].originalPrice
    })
    console.log(this.data.numpts);
    if (that.nums.goodnumss == 0) {
      that.countOnChangess = 1;
      for (var i = 0; i < that.payData.length; i++) {
        if (that.payData[i].id == 2) {
          that.payData.splice(i, 1);
        }
      }
      // console.log(that.payData)
    } else {
      if (that.payData.length == 0 || that.countOnChangess == 1) {
        that.payData.push({
          id: '2',
          num: 1,
          oldpric: that.gooodlist[2].originalPrice,
          price: that.gooodlist[2].originalPrice * this.data.play,
          name: that.gooodlist[2].name,
          coverPic: that.gooodlist[2].coverPic
        })
        that.countOnChangess = 0;
      }
      for (var i = 0; i < that.payData.length; i++) {
        if (that.payData[i].id == '3') {
          that.payData[i].num = that.nums.goodnumss
          that.payData[i].price =  that.gooodlist[2].originalPrice * this.data.play
        }
      }
      // console.log(that.payData)
    }
  },
  onChangessss(event) {
    var that = this.data
    console.log(event.detail,that.gooodlist);
    this.setData({
      nums: {
        goodnumssss: event.detail
      },
      baoyang: (event.detail-this.data.allnum ) * that.gooodlist[4].originalPrice
    })
    console.log(this.data.numpts);
    if (that.nums.goodnumssss == 0) {
      that.countOnChangess = 1;
      for (var i = 0; i < that.payData.length; i++) {
        if (that.payData[i].id == "7") {
          that.payData.splice(i, 1);
        }
      }
      // console.log(that.payData)
    } else {
      if (that.payData.length == 0 || that.countOnChangess == 1) {
        that.payData.push({
          id: '7',
          num: 1,
          oldpric: that.gooodlist[4].originalPrice,
          price: that.gooodlist[4].originalPrice * this.data.play,
          name: that.gooodlist[4].name,
          coverPic: that.gooodlist[4].coverPic
        })
        that.countOnChangess = 0;
      }
      console.log(that.payData);
      for (var i = 0; i < that.payData.length; i++) {
        if (that.payData[i].id == '7') {
          
          that.payData[i].num = that.nums.goodnumssss
          that.payData[i].price =  that.gooodlist[4].originalPrice * this.data.play
        }
      }
      // console.log(that.payData)
    }
  },
 

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})